package com.pig.controller;

import com.pig.aop.OperationLogger;
import com.pig.entity.AccountLogEntity;
import com.pig.entity.AccountUserEntity;
import com.pig.entity.AccountUserInfoEntity;
import com.pig.service.AccountUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import java.net.InetAddress;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;

@Controller
public class LoginAndRegister {


    /**
     * sercise
     */
    @Autowired
    private AccountUserService accountUserService;


    @Autowired
    private AccountUserEntity accountUserEntity2;

    /**
     * 日志类
     */
    @Autowired
    private AccountLogEntity accountLogEntity;

    /**
     * 用户登录
     */
    @RequestMapping("/user/index")
    @ResponseBody
    @OperationLogger(modelName = "/loginController", option = "登录")
    public String accountuserLoginController(HttpServletRequest request, Model model, HttpSession session) {
        //运行时间
        long startTime = System.currentTimeMillis();
        session.removeAttribute("msg");
        String accountName = request.getParameter("account_name");
        String password = request.getParameter("password");
        System.out.println("-----------------登录模式------------");
        System.out.println("name+" + accountName + "--pass+" + password);

        /*--------加密密码-------------*/
        String pass = DigestUtils.md5Hex(password);
        System.out.println("MD5加密后的字符串为--" + pass);
        AccountUserEntity accountUserEntity = new AccountUserEntity();
        accountUserEntity = accountUserService.login(accountName, pass, 1);
        System.out.println("accountUserEntity--" + accountUserEntity);

        if (null == accountUserEntity) {
            System.out.println("登录失败");
            //运行成功之后的时间
            long endTime = System.currentTimeMillis();
            float excTime = (float) (endTime - startTime) / 1000;
            System.out.println("执行时间：" + excTime + "s");
            return "100";
        }

        if (accountName.equals(accountUserEntity.getAccountName()) && pass.equals(accountUserEntity.getAccountPassword())) {
            //用户名保存到session 域里面
            session.setAttribute("accountName", accountName);
            System.out.println("登录成功");

            session.setAttribute("name", accountUserEntity.getAccountName());

            //运行成功之后的时间
            long endTime = System.currentTimeMillis();
            float excTime = (float) (endTime - startTime) / 1000;
            System.out.println("执行时间：" + excTime + "s");
            return "200";
        }
        return "500";
    }

    /**
     * 进入注册页面
     *
     * @return
     */
    @RequestMapping("/")
    public String registerAndLogin() {
        return "registerAndLogin";
    }

    /**
     * 提交 数据注册用户
     *
     * @param request
     * @param session
     * @return 注册成功就跳转到登录页面 ........
     */
    @RequestMapping("/accountUserRegister")
    @ResponseBody
    public String accountUserRegister(@ModelAttribute AccountUserEntity accountUserEntity, HttpServletRequest request, HttpSession session, BindingResult result, Model model) {
        // 运行时间
        long startTime = System.currentTimeMillis();
        //创建当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(startTime);

        String accountName = request.getParameter("accountName");
        String accountPassword2 = request.getParameter("accountPassword2");
        String accountEmail = request.getParameter("accountEmail");
        String accountPhone = request.getParameter("accountPhone");

        String accountPassword = DigestUtils.md5Hex(accountPassword2);
        System.out.println("------------欢迎进入注册-----------------");
        System.out.println(accountName + "--" + accountEmail + "--" + accountPhone + "--" + "--" + accountPassword2);
        System.out.println("MD5加密后的字符串为=" + accountPassword);


        //查询用户是否存在
        accountUserEntity.setAccountName(accountName);
        accountUserEntity = accountUserService.selectName(accountName);
        System.out.println("-----------------------------" + accountUserEntity + "-----------------------------");

        if (null != accountUserEntity) {
            System.out.println("用户存在");
            return "100";
        } else if (null == accountUserEntity) {
            System.out.println("------------------添加用户信息表----------------------");
            AccountUserInfoEntity accountUserInfoEntity = new AccountUserInfoEntity();
            accountUserInfoEntity.setAccountInfoEmail(accountEmail);
            accountUserInfoEntity.setAccountInfoPhone(accountPhone);
            accountUserInfoEntity.setAccountInfoImage("/staticfile/img/denglu.png");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time1 = sdf1.format(startTime);
            accountUserInfoEntity.setAccountInfoBirthday(time1);

            int insert = accountUserService.registerInfo(accountUserInfoEntity);
            int accountInfoId = accountUserInfoEntity.getAccountInfoId();
            System.out.println("accountInfoId---" + accountInfoId + "--------");
            if (insert > 0) {
                System.out.println("用户信息表添加成功");
            } else {
                System.out.println("用户信息表添加失败");
                session.setAttribute("msg", "用户信息表添加失败");
                return "500";
            }
            System.out.println("----------------------****************添加用户******************------------------------");
            System.out.println(accountName + "---------------------");
            accountUserEntity2.setAccountName(accountName);
            accountUserEntity2.setAccountBalabce(0.00d);
            accountUserEntity2.setAccountState(1);
            accountUserEntity2.setAccountNew(time);
            accountUserEntity2.setAccountPassword(accountPassword);
            accountUserEntity2.setAccountInfoId(accountInfoId);


            boolean flag = accountUserService.register(accountUserEntity2);
            System.out.println(accountUserEntity2);

            System.out.println(flag);
            if (flag) {
                System.out.println("注册成功");

                // 运行成功之后的时间
                long endTime = System.currentTimeMillis();
                float excTime = (float) (endTime - startTime) / 1000;
                System.out.println("执行时间：" + excTime + "s");
                return "200";
            } else {
                System.out.println("注册失败...........");
            }

        }

        System.out.println("注册失败.500..........");
        return "500";

    }


}
